package org.maxgroundstation.groundstation.view;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;

import org.maxgroundstation.groundstation.GroundStation;
import org.maxgroundstation.groundstation.MaxConstants;

/**
 * Mouse listener to allow mouse drag control of camera turret
 * in the video image panel.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class VideoImageMouseListener extends MouseAdapter {

    // Static members
    private static final int DEFAULT_WIDTH = 320;
    private static final int DEFAULT_HEIGHT = 240;
	
	// Data members.
    private GroundStation gs;
	private int xLast = 0;
    private int yLast = 0;
    private float panCache = 0F;
    private float tiltCache = 0F;
	
	/**
	 * Constructor
	 * @param service the video service.
	 */
	VideoImageMouseListener(GroundStation gs) {
		
		// Initialize data members.
		this.gs = gs;
	}
	
	/**
	 * Checks if the mouse listener should be enabled.
	 * @return true if enabled, false if disabled.
	 */
	private boolean isEnabled() {
		return gs.getInputPoller().useMouse();
	}
	
    @Override
    public void mousePressed(MouseEvent e) {
        if (isEnabled()) {
            xLast = e.getX();
            yLast = e.getY();
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (isEnabled()) {
            int xDrag = e.getX();
            int yDrag = e.getY();
            
            int imageWidth = 0;
            int imageHeight = 0;
            String videoServiceName = VideoDisplayUtil.getCurrentVideoServiceName();
            ImageIcon videoImage = VideoDisplayUtil.getCurrentVideoImage(videoServiceName);
            if (videoImage != null) {
                imageWidth = videoImage.getIconWidth();
                imageHeight = videoImage.getIconHeight();
            }
            
            // Send pan turret command with mouse drag.
            if (xDrag != xLast) {
                int xDiff = xDrag - xLast;
                if (imageWidth == 0) imageWidth = DEFAULT_WIDTH;
                float pixelWidthAngle = MaxConstants.CAMERA_VIEW_ANGLE / imageWidth;
                float newPan = panCache - (xDiff * pixelWidthAngle);
                if (newPan != panCache) {
                	gs.getCommandSender().pan(newPan);
                    panCache = newPan;
                }
                xLast = xDrag;
            }
            
            // Send tilt turret command with mouse drag.
            if (yDrag != yLast) {
                int yDiff = yDrag - yLast;
                if (imageHeight == 0) imageHeight = DEFAULT_HEIGHT;
                float pixelHeightAngle = MaxConstants.CAMERA_VIEW_ANGLE / imageHeight;
                float newTilt = tiltCache + (yDiff * pixelHeightAngle);
                if (newTilt > MaxConstants.MAX_HEAD_TILT_ANGLE) newTilt = MaxConstants.MAX_HEAD_TILT_ANGLE;
                else if (newTilt < MaxConstants.MIN_HEAD_TILT_ANGLE) newTilt = MaxConstants.MIN_HEAD_TILT_ANGLE;
                if (newTilt != tiltCache) {
                	gs.getCommandSender().tilt(newTilt);
                    tiltCache = newTilt;
                }
                yLast = yDrag;
            }
        }
    }
}